export const emailRegex =
    /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+\.)+[a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}))$/;

export const idCardRegex = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;

export const phoneRegex = /^1\d{10}$/;

export function validatePasswordFun(password: string) {
    if (!password) return false;
    // 长度至少为8位, 至少包含大小写字母、数字三种,不超过16位
    const reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[\s\S]{8,16}$/;
    if (!reg.test(password)) {
        return false;
    }
    return true;
}

export const phoneValidator = (isRequired = true) => {
    if (isRequired) {
        return (rule: any, value: any, callback: any) => {
            if (!value) {
                callback(new Error("请输入手机号"));
            }
            if (value && !phoneRegex.test(value)) {
                return callback(new Error("请输入正确的手机号!"));
            }
            return callback();
        };
    }
    return (rule: any, value: any, callback: any) => {
        if (value && !phoneRegex.test(value)) {
            return callback(new Error("请输入正确的手机号!"));
        }
        return callback();
    };
};

export const emailValidator = (rule: any, value: any, callback: any) => {
    if (!value) {
        return callback(new Error("请输入邮箱"));
    }
    if (value && !emailRegex.test(value)) {
        return callback(new Error("请输入正确的邮箱!"));
    }
    return callback();
};

export const emailArrayValidator = (rule, value, callback) => {
    if (!Array.isArray(value) || !value.length) {
        callback();
        return;
    }
    for (const mail of value) {
        if (!emailRegex.test(mail)) {
            callback(new Error(`存在不合法的邮箱地址${mail}，请修改!`));
            return;
        }
    }
    callback();
};

export function intFormatter(value: string) {
    return value.replace(/[^0-9]/g, "");
}

export const attachValidator = (_: any, value: any, callback: any) => {
    if (!value || value.length === 0) {
        callback(new Error("请上传附件"));
    } else {
        callback();
    }
};

export const downloadFile = (res: { fileName: string; data: string }) => {
    if (!res.fileName || !res.data) {
        return;
    }
    const content = res?.data;
    const fileName = res?.fileName;
    const blob = new Blob([content]);

    // console.log(content);
    const elink = document.createElement("a"); //创建一个a标签通过a标签的点击事件区下载文件
    elink.download = fileName;
    elink.style.display = "black";
    elink.href = URL.createObjectURL(blob); //使用blob创建一个指向类型数组的URL
    document.body.appendChild(elink);
    elink.click();
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink);
};

// 解析JSON内容
export const parseJson = (content: string) => {
    let res: any = {};
    try {
        res = JSON.parse(content);
    } catch (error) {
        console.log("解析json字符串出错");
        res = {};
    }
    return res;
};

let time: number;
export const debounce = (fn: Function, timer = 50) => {
    const curr = new Date().getTime();
    if (!time || curr - time >= timer) {
        time = curr;
        fn();
        return;
    }
};
// 获取图片搜索列表
export const queryImageSuggestions = (queryString: string, cb: (arg: any) => void) => {
    if (!queryString) {
        cb([]);
    } else {
        try {
            // getImageSearch(queryString).then((res) => cb(res));
        } catch (error) {
            cb([]);
        }
    }
};
